package cn.briarbear.myblog.controller.admin;

import cn.briarbear.myblog.entity.dto.AboutModifyForm;
import cn.briarbear.myblog.entity.dto.BlogAddForm;
import cn.briarbear.myblog.entity.dto.IdListForm;
import cn.briarbear.myblog.entity.dto.ResponseStatus;
import cn.briarbear.myblog.entity.table.About;
import cn.briarbear.myblog.entity.table.Blog;
import cn.briarbear.myblog.entity.vo.BlogFormView;
import cn.briarbear.myblog.entity.vo.BlogModifyView;
import cn.briarbear.myblog.enums.ViewConsts;
import cn.briarbear.myblog.service.IAboutService;
import cn.briarbear.myblog.service.IBlogService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private IBlogService blogService;
    @Autowired
    private IAboutService aboutService;
    /**
     * 博客管理首页
     */
    @RequestMapping({"","/index","/blog_manage"})
    public String adminIndex(HttpServletRequest request, Model model){
        return "admin/blog_manage";
    }

    /**
     * 写博客页面-新建博客文章
     */
    @GetMapping("/blog_add")
    public String adminBlogAdd(HttpServletRequest request,Model model){
        return "admin/blog_add";
    }


    /**
     * Post请求
     * @param blogAddForm
     * @return
     */
    @PostMapping("/blog_add")
    public String adminBlogAdd(BlogAddForm blogAddForm){

        blogService.blogAdd(blogAddForm);

        return "redirect:/admin";
    }

    /**
     * 后台加载博客列表
     * @return 文章表格视图类 列表
     */
    @GetMapping("/blog_list")
    @ResponseBody  //以对象返回
    public Object adminBlogList(){
        List<BlogFormView> viewList = blogService.getAdminBlogFormList();
        return viewList;
    }

    /**
     * Get请求，处理修改博客的跳转
     * @return
     */
    @GetMapping("/blog_modify/{id}")
    public String adminBlogModify(@PathVariable Integer id,Model model){

        Blog blog = blogService.getBlogById(id);  //根据id从后台获取该博客文章
        BlogModifyView blogModifyView = new BlogModifyView(blog);
        model.addAttribute(ViewConsts.VIEW_BLOG,blogModifyView);
        return "admin/blog_modify";

    }

    /**
     * Post提交 博客修改
     * @param blogModifyView
     * @return
     */
    @PostMapping("/blog_modify")
    public String adminBlogModify(BlogModifyView blogModifyView){

        blogService.blogModify(blogModifyView);
        return "redirect:/admin/index";
    }

    /**
     * @RequestBody对json格式的参数转换为Java类型
     * @param request
     * @param IdListForm
     * @return
     */
    @DeleteMapping("/blog_delete")
    @ResponseBody
    public Object adminBlogDelete(HttpServletRequest request,@RequestBody IdListForm IdListForm){

        String json = JSON.toJSONString(IdListForm);
        System.out.println(json);
        blogService.blogDelete(IdListForm);

        return new cn.briarbear.myblog.entity.dto.ResponseStatus(201,false);
    }


    @GetMapping("/about_modify")
    public String adminAboutModify(Model model){
        About about = aboutService.getAbout();
        AboutModifyForm form = new AboutModifyForm(about);
        model.addAttribute(ViewConsts.VIEW_ABOUT,about);
        return "admin/about_modify";
    }

    @PostMapping("/about_modify")
    @ResponseBody
    public Object updateAbout(AboutModifyForm form, @ModelAttribute(binding = false) About about){
//        aboutService.updateAbout(form,about);
        aboutService.aboutModify(form);
        return new ResponseStatus(201,false);
    }
}
